<?php
/**
 * Created by dzw.
 * User: Administrator
 * Date: 2018-03-16
 * Time: 10:22
 */

namespace App\Http\Controllers\Web\Member\Buyer;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;

class ChangeController extends BaseController
{
    protected $api_url = [
        'show'  => ['get', 'change'],
        'index' => ['get', 'change'],
        'store' => ['post','change'],
        'create'=> ['get', 'buyer/orders/%d'],
        'put'   => ['put', 'change'],
    ];
    protected $api_host;

    protected $request;

    public function __construct(Request $request)
    {
        $this->request  = $request;
        $this->api_host = config('url.api_web');
    }

    public function indexAction()
    {

        $this->validate($this->request, [
            'page' => 'integer',
            'status' => 'integer',
        ]);

        //参数部分
        if($this->request->input('status')==6){
            $search_params = $this->request->all(['keyword', 'shop_name','change_product']);
        }else{
            $search_params = $this->request->all(['keyword', 'status','shop_name','change_product']);
        }

        $page = $this->request->get('page', 1);

        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'page' => $page,
            'orderBy'=>'id',
            'sortedBy'=>'desc',
            //'with' => 'skus',
        ];
        //请求部分
        $ret =  http_web($this->api_url['index'])->send($params);

        //结果处理部分
        $data = [];
        if(isset($ret['code']) && $ret['code']===0 && !empty($ret['data'])){
            $data= paginate($this->request, $ret['data']);
        }

        //回填搜索框值
        $data['search_params'] = $search_params;
        
        return view('web.user.buyer.return_bill', $data);

    }

    public function createAction($id)
    {
        //参数部分
        $data = [
            'with' => 'shop;orderProducts;user'
        ];
        //请求部分

        $data['user_id'] = 2;
        $ret =  http_web($this->api_url['create'],[$id])->send($data);

        $ret['data']['change_product']=[];
        foreach($ret['data']['order_products'] as $k=>$v){
            $data = [
                'product_name' => $v['product_name'],
                'quantity'     => $v['quantity'],
                'product_money'=> $v['product_money'],
                'shop_name'    => $v['shop_name'],
                'order_code'   => $v['order_code'],
            ];
            array_push($ret['data']['change_product'],$data);
        }
        $ret['data']['change_product'] = json_encode($ret['data']['change_product']);
        //结果处理部分
        return view('web.user.buyer.repair', $ret['data']);

    }
    public function storeAction()
    {
        $data['user_id'] = 2;
        $data = $this->request->all();

        //请求部分
        $ret =  http_web($this->api_url['create'])->send($data);

        //结果处理部分
        return response()->json($ret);
    }



}